<?php
	check_login();
	$a = getAction();
		
	switch ($a) 
	{
		case "index":
			$url = "admin.php";
			$sql = " select * from " .table('admin')
				 . " WHERE 1=1 "
				 . " order by id desc";
			
			//分页
			$rscount = $db->getOne("select count(1) from (" . $sql . ") temp");
			$page = max(1, intval($_GET['page']));
			$start = ($page-1) * PAGE_SIZE;
			$pagelist = multipage($rscount, PAGE_SIZE, $page, $url);
			$sql .= " limit $start, " . PAGE_SIZE;
			
			$smarty->assign("pagelist", $pagelist);
			$smarty->assign("login_id", $_SESSION['web_admin']['login_id']);
				
			$smarty->assign("adminlist",$db->getAll($sql));
			$smarty->display("admin.html");

			break;
		
		case "add":
			$id = get_post("id");
			
			if($_POST) {
				$formData = $_POST["formData"];
				$nowTime = getNowTime();
				
				if (!$formData["login_id"]) {
					errback("用户名不能为空！");
				}
				
				if(!$id) {
					cklong($formData["password"],"密码长度介于7-50",50,7);
				}
				
				$sql = "select login_id from ".table('admin')." where login_id='" . $formData["login_id"] . "' ";
				if($id) {
					$sql .= " and id <> $id";
				}

				$ct = $db->getOne($sql);
				
				if($ct) errback("该用户名已被注册");
				
				if($id) {
					$formData["id"] = $id;
					$password = $db->getOne("select password from ".table('admin')." where login_id='" . $formData["login_id"] . "'");
					
					if($formData["password"]) {
						$formData["password"] = md5($formData["password"]);
					} else {
						$formData["password"] = $password;
					}
					$formData["update_time"] = $nowTime;
					$db->update("admin", $formData, " and id = $id ");
				} else {
					$formData["password"] = md5($formData["password"]);
					$formData["insert_time"] = $nowTime;
					$formData["update_time"] = $nowTime;
					$db->insert("admin", $formData);
				}
				
				gourl("admin.php?m=admin");
			} else {
				if($id) {
					$formData = $db->getRow("select * from " . table('admin') . " where id = $id ");
					$smarty->assign("formData", $formData);
				}
				
				$smarty->display("admin_add.html");
			}
			
			break;

		case "del":
			$id = intval($_GET['id']);
			$db->query("delete from ".table('admin')." where id='$id' and login_id <> 'admin'");
			gourl();
				
			break;

		case "log":
			$url = "admin.php?m=admin&a=log";
			$sql = " select * from " .table('admin_login_log')
				 . " order by id desc";
		
			//分页
			$rscount = $db->getOne("select count(*) from (" . $sql . ") temp");
			$page = max(1, intval($_GET['page']));
			$start = ($page-1) * PAGE_SIZE;
			$pagelist = multipage($rscount, PAGE_SIZE, $page, $url);
			$sql .= " limit $start, " . PAGE_SIZE;
									
			$smarty->assign("pagelist", $pagelist);
			$smarty->assign("list",$db->getAll($sql));

			$smarty->display("admin_log.html");

			break;
			
		case "logdel":
			$id = intval($_GET['id']);
			$db->query("delete from ".table('admin_login_log')." where id='$id' ");
			gourl();
				
			break;
	}

?>

